#!/bin/bash
# This program will expand a userdefined degree of a binomial in the form (x+1)^a, where a is the degree of the binomial.
# The program will first print the triangle and then secondly will print the binomial
count=1
set 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

echo "What degree? (1-20): \c"
read depth
exp="$depth"

if [[ $depth -le 20 && $depth -gt 0 ]]
then 
	clear
	echo "Pascals Triangle: "
	echo 1
	while [ $count -le $depth ] #Creates the proper triangle line
	do
		set 0 `expr $1 + $2` `expr $2 + $3` `expr $3 + $4` `expr $4 + $5` `expr $5 + $6` `expr $6 + $7` `expr $7 + $8` `expr $8 + $9` `expr $9 + ${10}` `expr ${10} + ${11}` `expr ${11} + ${12}` `expr ${12} + ${13}` `expr ${13} + ${14}` `expr ${14} + ${15}` `expr ${15} + ${16}` `expr ${16} + ${17}` `expr ${17} + ${18}` `expr ${18} + ${19}` `expr ${19} + ${20}` `expr ${20} + ${21}` `expr ${21} + ${22}`
	
		for item in $* #PRINTS TRIANGLE
		do
			case $item in
			[0])
			;;
			[1-9]*)
				echo "$item \c"
			;;
			esac
		done
		echo
		count=`expr $count + 1`
	done
	
	shift 1 #removes leading 0
	echo 
	echo "Binomial Expansion (x+1)^ \b$exp \b:"
	
	for item in $* #PRINTS EXPANDED BINOMIAL
	do
		case $item in
		[0])
			break #drops out when hitting final zero
		;;
		[1-9]*)
			if [[ $exp -gt 0 ]]
			then
				echo "$item \bx^ \b$exp + \c" #properly formats output, slash-b is backspace, slash-c is suppress-newline
				exp=`expr $exp - 1`
			else
				echo "$item" #echos the final 1 without it looking like 1x^0
			fi
		;;	
		esac
	done
	echo
	echo





else
	echo "Invalid Input, please try again."
fi


exit
